export default {
  validateMobile(mobile) {
    if (mobile.length != 11) {
      return false;
    }
    const myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(16[0-9]{1})|(17[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(19[0-9]{1}))+\d{8})$/;
    if (!myreg.test(mobile)) {
      return false;
    }

    return true;
  },
  // 年月日
  getDay(time) {
    if (!time) return '/'
    var date = new Date()
    date.setTime(time)
    var year = date.getFullYear()
    var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
    var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
    return year + '-' + month + '-' + day
  },
  // 年月日 时分 2023-01-29 14:28:59
  getDate(time, slice = true) {
    if (!time) return ''
    if (slice) {
      time = time.slice(0, 16)
    }
    time = time.replace('-', '年').replace('-', '月').replace(' ', '日 ')
    return time
  },
  formatRichText(nodes = "") {
    return nodes.replace(/<p><\/p>/g, '<br/>')
      .replace(/<pre/g, '<pre style="max-width: 100%;max-height: 100%;margin: 10px 0;padding: 15px;overflow: auto;background-color: #f1f2f3;border-radius: 3px;color: #666;font-family: monospace;font-size: 14px;font-weight: 400;line-height: 16px;word-wrap: break-word;white-space: pre-wrap;"')
      .replace(/<blockquote/g, '<blockquote style="margin: 0 0 10px;padding: 15px 20px;background-color: #f1f2f3;border-left: 5px solid #ccc;color: #666;font-style: italic;"')
      .replace(/\<img/g, '<img style="width: 100%; height: auto;display: block;"')
      .replace(/\pre/g, 'p');
  },
  debounce: (fn, interval = 500) => {
    let timer;
    return function () {
      clearTimeout(timer);
      let context = this;
      let args = arguments;
      timer = setTimeout(function () {
        fn.call(context, ...args);
      }, interval);
    };
  },
  getQuery: function (url) {
    let obj = {}
    let str = url.slice(url.indexOf('?') + 1)
    let arr = str.split('&')
    for (let j = arr.length, i = 0; i < j; i++) {
      let arr_temp = arr[i].split('=')
      obj[arr_temp[0]] = arr_temp[1]
    }
    return obj
  }
}